package com.weather.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.weather.domain.TempHumi;
import com.weather.mapper.TempHumiMapper;
import com.weather.service.MailService;
import com.weather.service.TempHumiService;
import com.weather.websocket.WebSocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Service
public class TempHumiServiceImpl extends ServiceImpl<TempHumiMapper, TempHumi> implements TempHumiService {

    @Autowired
    private MailService mailService;

    @Autowired
    private TempHumiMapper tempHumiMapper;

//    收件邮箱地址
    @Value("${TempHumi.ToEmail}")
    private String ToEmail;

//    最大监控温度
    @Value("${TempHumi.maxtemp}")
    private BigDecimal maxtemp;

//    最大监控湿度
    @Value("${TempHumi.maxhumi}")
    private BigDecimal maxhumi;

//    最小监控温度
    @Value("${TempHumi.mintemp}")
    private BigDecimal mintemp;

//    最小监控湿度
    @Value("${TempHumi.minhumi}")
    private BigDecimal minhumi;

//    @Override
//    public boolean saveAndCheckTemperature(TempHumi tempHumi) {
//        BigDecimal temp = tempHumi.getTemperature();
//
//        String sendTo = ToEmail;
//        String subject = "温度异常";
//        String text30 = "温度过高，请及时检查！！！当前温度为：" + temp + " 当前湿度为：" + tempHumi.getHumidity();
//        String text25 = "温度过低，请及时检查！！！当前温度为：" + temp + " 当前湿度为：" + tempHumi.getHumidity();
//
//        if (temp.compareTo(maxtemp) > 0) {
//            mailService.sendSimpleMail(sendTo, subject, text30);
//        } else if (temp.compareTo(mintemp) < 0) {
//            mailService.sendSimpleMail(sendTo, subject, text25);
//        }
//        System.out.println("tempHumi = " + tempHumi);
//
//
//        String json = "{\"type\":\"chatUpdate\",\"content\":tempHumi}";
//        String userId = "1000001";
//
//        WebSocketHandler.sendMsgToUser(userId, json);
//
//
//        return tempHumiMapper.insert(tempHumi) > 0;
//    }


    @Override
    public boolean saveAndCheckTemperature(TempHumi tempHumi) {
        BigDecimal temp = tempHumi.getTemperature();

        String sendTo = ToEmail;
        String subject = "温度异常";
        String text30 = "温度过高，请及时检查！！！当前温度为：" + temp + " 当前湿度为：" + tempHumi.getHumidity();
        String text25 = "温度过低，请及时检查！！！当前温度为：" + temp + " 当前湿度为：" + tempHumi.getHumidity();

        if (temp.compareTo(maxtemp) > 0) {
            mailService.sendSimpleMail(sendTo, subject, text30);
        } else if (temp.compareTo(mintemp) < 0) {
            mailService.sendSimpleMail(sendTo, subject, text25);
        }

        System.out.println("tempHumi = " + tempHumi);

        try {
            // ✅ 创建 ObjectMapper 对象
            ObjectMapper mapper = new ObjectMapper();

            // ✅ 构建消息结构
            Map<String, Object> message = new HashMap<>();
            message.put("type", "chatUpdate");
            message.put("content", tempHumi); // 自动序列化为 JSON

            // ✅ 转为 JSON 字符串
            String json = mapper.writeValueAsString(message);

            // ✅ 发送 WebSocket 消息
            WebSocketHandler.sendMsgToUser("1000001", json);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return tempHumiMapper.insert(tempHumi) > 0;
    }


}
